package com.rentdvd.client.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rentdvd.ws.RentDVDImplProxy;

@WebServlet(name = "LoginServlet", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		RentDVDImplProxy service = new RentDVDImplProxy();
		
		boolean isLogged = service.authenticate(username, password);
		
		if (isLogged) {
			request.getSession().setAttribute("username", username);
			
			request.getRequestDispatcher(("/catalog.jsp")).forward(request, response);
		} else {
			request.setAttribute("error", 1);
			
			request.getRequestDispatcher(("/login.jsp")).forward(request, response);
		}
	}
}